<?php
// +----------------------------------------------------------------------
// | Description: 客户
// +----------------------------------------------------------------------
// | Author: zl
// +----------------------------------------------------------------------

namespace app\crm\controller;

use app\admin\controller\ApiCommon;
use app\crm\model\BalanceLog;
use app\crm\model\Customer as CustomerModel;

use app\crm\model\CustomerConfig as CustomerConfigModel;
use think\response\Json;
use think\facade\Db;

use app\crm\model\MedicalRecords;


class CustomerCase extends ApiCommon
{
    protected $customerModel;
    protected $customerConfigModel;
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
     **/
    public function initialize()
    {
        $action = [
            'permission' => [''],
            'allow' => ['users', 'info']
        ];
        event('CheckAuth',$action);
        $request = $this->request;
        $a = strtolower($request->action());

        $this->customerModel = new CustomerModel();
        $this->customerConfigModel = new CustomerConfigModel();
        if (!in_array($a, $action['permission'])) {
            parent::initialize();
        }
    }

    /**
     * 列表
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function list(MedicalRecords $MedicalRecordsModel): Json
    {
        $shopIds = $this->getCommonShopIdsCollectByUserType();
        $this->param['shopIds'] = $shopIds;
        $this->param['user_id'] = $this->userInfo['id'];
        $this->param['shop_person_id'] = $this->userInfo['shop_person_id'];
        $MedicalRecordsModel = new MedicalRecords();
        $data = $MedicalRecordsModel->getDataList($this->param);
        if (!$data) {
            return resultArray(['error' => $MedicalRecordsModel->getError()]);
        }

        return resultArray(['data' => $data]);
    }

    /**
     * 添加病例
     * @param  CustomerModel $customerModel
     * @return Json
     */

    public function addCase(MedicalRecords $MedicalRecordsModel): Json
    {
        try{
            $shopIds = $this->getCommonShopIdsCollectByUserType();
            $this->param['shopIds'] = $shopIds;
            $this->param['user_id'] = $this->userInfo['id'];
            $this->param['shop_person_id'] = $this->userInfo['shop_person_id'];

            $MedicalRecordsModel = new MedicalRecords();
            $data = $MedicalRecordsModel->addCase($this->param);
            if (!$data) {
                return resultArray(['error' => $MedicalRecordsModel->getError()]);
            }
            return resultArray(['data' => $data]);
        }catch (\Exception $e){
            return resultArray(['error' => $e->getMessage()]);
        }

    }



    /**
     * 修改病例
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function saveCase(MedicalRecords $MedicalRecordsModel): Json
    {
        try{
            $shopIds = $this->getCommonShopIdsCollectByUserType();
            $this->param['shopIds'] = $shopIds;
            $this->param['user_id'] = $this->userInfo['id'];
            $this->param['shop_person_id'] = $this->userInfo['shop_person_id'];

            $MedicalRecordsModel = new MedicalRecords();
            $data = $MedicalRecordsModel->saveCase($this->param);
            return resultArray(['data'=>[],'error'=>'创建成功']);
        }catch (\Exception $e){
            return resultArray(['error' => $e->getMessage()]);
        }

    }

    /**
     * 病例详情
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function caseDetail(MedicalRecords $MedicalRecordsModel): Json
    {
        try{
            $MedicalRecordsModel = new MedicalRecords();
            $data = $MedicalRecordsModel->caseDetail($this->param);
            return resultArray(['data'=>$data,'error'=>'查看详情']);
        }catch (\Exception $e){
            return resultArray(['error' => $e->getMessage()]);
        }

    }

    /**
     * 病例变更记录
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function caseChangeLog(MedicalRecords $MedicalRecordsModel): Json
    {
        $MedicalRecordsModel = new MedicalRecords();
        $data = $MedicalRecordsModel->caseChangeLog($this->param);
        if (!$data) {
            return resultArray(['error' => $MedicalRecordsModel->getError()]);
        }

        return resultArray(['data' => $data]);
    }

}
